package com.scpii.universal.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;

public class Client {
	private static String IP = "localhost:8080";
//	private static String IP = "api.gotoapp.cn";
//	private static String IP = "61.188.177.231";
//	private static String IP = "61.188.177.233";
	private static ZipUnZip zipUnZip = new ZipUnZip();
	//以POST方法访问网络
	public static byte[] testPOSTMethod(int cmd,byte[] bytes) throws Exception{
		byte[] dataByte = null;
		HttpClient httpClient = new DefaultHttpClient();
		//设置参数
		List<NameValuePair> namePairsList = new ArrayList<NameValuePair>();
		HttpPost httpPost = new HttpPost("http://"+IP+"/UniversalApi/Handler?cmd="+cmd);
//		HttpPost httpPost = new HttpPost("http://"+IP+"/unapi/Handler?cmd="+cmd);
		System.out.println(httpPost.getURI());
		try {
			//设置传递的字节流数据
			httpPost.setEntity(new ByteArrayEntity(bytes));
			//执行请求
			HttpResponse httpResponse = httpClient.execute(httpPost);
			//获取返回的数据
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				bufferedHttpEntity.writeTo(byteArrayOutputStream);
				byte[] responseBytes = byteArrayOutputStream.toByteArray();
				dataByte = responseBytes;
				httpPost.abort();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return dataByte;
	}
	
	//以GET方法访问网络
	public static byte[] testGETMethod(String actionMethod,String[] params) throws Exception{
		byte[] dataByte = null;
		HttpClient httpClient = new DefaultHttpClient();
		List<NameValuePair> namePairsList = new ArrayList<NameValuePair>();
		for (int i = 0; i < params.length; i++) {
			if (params.length==1) {
				namePairsList.add(new BasicNameValuePair("param1",params[i]));
			}else {
				namePairsList.add(new BasicNameValuePair("param"+(i+1),params[i]));
			}
		}
		UrlEncodedFormEntity uee = new UrlEncodedFormEntity(namePairsList,HTTP.UTF_8);
		HttpGet httpGet = new HttpGet("http://"+IP+"/UniversalApi/Handler/");
		try {
			HttpParams httpParams = new BasicHttpParams();
			HttpResponse httpResponse = httpClient.execute(httpGet);
			httpResponse.setParams(httpParams);
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(httpEntity);
				ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
				bufferedHttpEntity.writeTo(byteArrayOutputStream);
				byte[] responseBytes = byteArrayOutputStream.toByteArray();
				responseBytes = zipUnZip.unGZip(responseBytes);
				dataByte = responseBytes;
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return dataByte;
	}
}
